﻿/// <reference path="../../scripts/jasmine.js" />
/// <reference path="../../scripts/3.x/CrmRestKit.js" />

/*globales CrmRestKit, 
    jasmine, describe, it, expect, runs, waitsFor */

///
/// Integration-tests for the CrmRestKit-3
///
( function () {
    'use strict';

    describe( 'The module allows the loading of single records', function () {

        it( 'Jasmine-Test-Runner is running...', function () {

            expect(jasmine).toBeDefined();
        } );

    } );

    describe( 'The module support the creation of new entity-records', function () {

        it( 'should create a single new account-record', function () {

            // arrange 
            var account = { Name: 'unit-testing' },
                created = null,
                errorSpy = jasmine.createSpy( 'error-handler' );

            // action 
            CrmRestKit.Create( 'Account', account ).then( function ( data ) {
                created = data.d;
            }, errorSpy );

            waitsFor( function () {
                return created !== null;
            },50 );

            runs( function () {
                // assert
                expect( created.CreatedOn ).toBeDefined();
                expect( errorSpy ).not.toHaveBeenCalled();
            } );

        } );

        it( 'should support special characters', function () {

            // arrange 
            var account = { Name: 'unit-testing-äöüß$' },
                created = null,
                errorSpy = jasmine.createSpy( 'error-handler' );

            // action 
            CrmRestKit.Create( 'Account', account ).then( function ( data ) {
                created = data.d;
            }, errorSpy );

            waitsFor( function () {
                return created !== null;
            }, 500 );

            runs( function () {

                // assert
                expect( created.Name ).toBe(account.Name);
                expect( errorSpy ).not.toHaveBeenCalled();
            } );
        } );
    } );

    describe( 'The module allows the altering of records', function () {

        it( 'should update the name to "integration-testing|after-update"', function () {
           
            var modName = 'integration-testing|after-update',
                updateTarget = null,
                errorSpy = jasmine.createSpy( 'error-handler' );

            CrmRestKit.Create( 'Account', { Name: 'integration-tests' } ).then( function ( data ) {
                updateTarget = data.d;
            }, errorSpy );

            waitsFor( function () {
                return updateTarget !== null;
            } );

            runs( function () {
                
                CrmRestKit.Update( 'Account', updateTarget.AccountId, { Name: modName }, false ).fail(errorSpy);
            } );

            runs( function () {

                var updated = null;

                CrmRestKit.Retrieve( 'Account', updateTarget.AccountId, ['Name'], false ).then( function ( data ) {
                    updated = data.d;
                } );

                waitsFor( function () {
                    return updated !== null;
                } );
                
                runs( function () {

                    // assert 
                    expect( errorSpy ).not.toHaveBeenCalled();
                    expect(updated.Name).toBe(modName);
                } );
                
            } );
        } );

    } );

}() );